package com.wjzjq.hiring.company.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wjzjq.hiring.common.DBObject;
import com.wjzjq.hiring.common.util.DBUtil;
import com.wjzjq.hiring.company.entity.CompanyEntity;
import com.wjzjq.hiring.company.entity.PositionEntity;
import com.wjzjq.hiring.talent.entity.TalentEntity;

public class CompanyDao {
	public static CompanyEntity login(String code, String password) {
		CompanyEntity company = null;
		DBObject dbObject = DBUtil.executeQuery("SELECT * FROM company WHERE code=? AND password=?", code, password);
		ResultSet resultSet = dbObject.getResultSet();
		try {
			if (resultSet.next()) {
				company = new CompanyEntity();
				company.setId(resultSet.getInt("id"));
				company.setName(resultSet.getString("name"));
				company.setPhone(resultSet.getString("phone"));
				company.setPassword(password);
				company.setEmail(resultSet.getString("email"));
				company.setCode(code);
				company.setSlogan(resultSet.getString("slogan"));
				company.setLinkman(resultSet.getString("linkman"));
				company.setAddress(resultSet.getString("address"));
				company.setNumbers(resultSet.getInt("numbers"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return company;

	}
	
	public  int update(CompanyEntity company) {
		int row = 0;

		String sql = "update company set password=?,name=?,phone=?,code=?,email=?,linkman=?,address=?,numbers=?,slogan=? where id=?";
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(company.getPassword());
		paramList.add(company.getName());
		paramList.add(company.getPhone());
		paramList.add(company.getCode());
		paramList.add(company.getEmail());
		paramList.add(company.getLinkman());
		paramList.add(company.getAddress());
		paramList.add(company.getNumbers());
		paramList.add(company.getSlogan());
		paramList.add(company.getId());
		row = DBUtil.executeUpdate(sql, paramList);
		System.out.println("update");
		return row;
	}

	public static List<PositionEntity> getPL(int companyId) {
		// TODO Auto-generated method stub
		String sql = "select * from position where company_id=?";
		DBObject dbObject = DBUtil.executeQuery(sql, companyId);
		ResultSet rs = dbObject.getResultSet();
		List<PositionEntity> pl = new ArrayList();
		try {
			while (rs.next()) {
				PositionEntity pe = new PositionEntity();
				pe.setId(rs.getInt("id"));
				pe.setName(rs.getString("name"));
				pe.setDuty(rs.getString("duty"));
				pe.setAbility(rs.getString("ability"));
				pe.setSalary1(rs.getInt("salary1"));
				pe.setSalary2(rs.getInt("salary2"));
				pe.setReleaseTime(rs.getTimestamp("release_time"));
				pl.add(pe);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(dbObject);
		}
		return pl;
	}

	public List<CompanyEntity> listAllCompany() {
		// TODO Auto-generated method stub
		String sql = "select * from company";
		DBObject dbObject = DBUtil.executeQuery(sql);
		ResultSet rs = dbObject.getResultSet();
		List<CompanyEntity> companyList = new ArrayList();
		try {
			while (rs.next()) {
				CompanyEntity c = new CompanyEntity();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setPhone(rs.getString("phone"));
				c.setEmail(rs.getString("email"));
				c.setSlogan(rs.getString("slogan"));
				c.setLinkman(rs.getString("linkman"));
				c.setNumbers(rs.getInt("numbers"));
				c.setAddress(rs.getString("address"));
				companyList.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(dbObject);
		}
		return companyList;
	}

	
	public static int register(CompanyEntity company) {
		// TODO Auto-generated method stub
		int row = 0;
		String sql = "insert into company values(null,?,?,?,?,?,?,?,?,?)";
		List<Object> paramList = new ArrayList();
		paramList.add(company.getCode());
		paramList.add(company.getPassword());
		paramList.add(company.getName());
		paramList.add(company.getEmail());
		paramList.add(company.getPhone());
		paramList.add(company.getLinkman());
		paramList.add(company.getAddress());
		paramList.add(company.getNumbers());
		paramList.add(company.getSlogan());
		row = DBUtil.executeUpdate(sql, paramList);
		return row;
	}
	
	public CompanyEntity getCompanyByEmail(String email) {
		// TODO Auto-generated method stub
		CompanyEntity company = null;
		DBObject dbObject = DBUtil.executeQuery("select * from company where email=?", email);
		ResultSet rs = dbObject.getResultSet();
		try {
			if (rs.next()) {
				company = new CompanyEntity();
				company.setPassword(rs.getString("password"));
				company.setCode(rs.getString("code"));
				company.setEmail(rs.getString("email"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return company;
	}
	
	public int resetpwd(String code, String newpassword) {
		// TODO Auto-generated method stub
		int row = 0;
		row = DBUtil.executeUpdate("update company set password=? where code=?",newpassword , code);
		return row;
	}
}
